
<!-- 办公室减碳编辑业务实现-->
<template>
  <!-- 新增&修改 start-->
  <vxe-modal :modelValue="isEditFormVisible" :show-footer="true" :position="dialogPosition" :title="title" :width="dialogWidth" :height="dilogHeight" resize="" :before-hide-method="beforeCloseDialog">
    <el-form inline-message status-icon ref="dataFormRef" :rules="verify" :model="formData" label-position="left" label-width="90px">
      <el-form-item label="主键" prop="id" v-show="false">
        <el-input v-model="formData.id" :readonly="isReadOnly" />
      </el-form-item>
      <el-form-item label="办公室">
        <lxsh-common-comb v-model="formData.officeName" :requestUrl="requestContextPath.cer + '/cer/officeData/getComboList'" :columnInfo="{columnName:'办公室',readonly:isReadOnly}" />
      </el-form-item>
      <el-form-item label="周次" prop="week">
        今年的第
        <el-input-number v-model="formData.week" :min="1" :max="52" @change="handleWeekChange" :columnInfo="{columnName:'周次',readonly:isReadOnly}" />
        周
        <!-- <el-input type="number" :controls="false" v-model.number="formData.week" @change="handleWeekChange" :columnInfo="{columnName:'周次',readonly:isReadOnly}" >
        <template #prepend>今年的第</template>
         <template #append>周</template>
        </el-input> -->
      </el-form-item>
      <el-form-item label="开始时间" prop="stratTime">
        <el-date-picker v-model="formData.stratTime" value-format="YYYY-MM-DD 00:00:00" type="date" :columnInfo="{columnName:'开始时间',readonly:isReadOnly}" />
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker v-model="formData.endTime" value-format="YYYY-MM-DD 00:00:00" type="date" :columnInfo="{columnName:'结束时间',readonly:isReadOnly}" />
      </el-form-item>
      <el-form-item label="开灯时长" prop="lightTime">
        <el-input type="number" :controls="false" v-model.number="formData.lightTime" :columnInfo="{columnName:'开灯时长',readonly:isReadOnly}" @change="inputChange" >
        <template #prepend>时间范围内累计</template>
        <template #append>小时</template>
        </el-input>
      </el-form-item>
      <el-form-item label="空调时长" prop="airTime">
        <el-input type="number" :controls="false" v-model.number="formData.airTime" :columnInfo="{columnName:'空调时长',readonly:isReadOnly}" @change="inputChange" >
        <template #prepend>时间范围内累计</template>
        <template #append>小时</template>
        </el-input>
      </el-form-item>
      <el-form-item label="减碳量" prop="reduceCount">
        <el-input type="number" :controls="false" v-model.number="formData.reduceCount" :columnInfo="{columnName:'减碳量',readonly:isReadOnly}" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="beforeCloseDialog()">取消</el-button>
      <el-button type="primary" @click="onSubmit(dataFormRef)">确定</el-button>
    </template>
  </vxe-modal>
  <!-- 新增&修改 end-->
</template>
<script lang="ts" setup>
import { hooksForCommonForm } from "hhlt-vue/src/components/Lxsh/Common/hooksForCommonForm";
import { Ref, ref, toRefs, onBeforeMount } from "vue";
import { FormInstance } from "element-plus";
import {
  ICerOfficeImpro,
  CerOfficeImpro,
} from "lxsh-cer-web-pc/src/model/cer/cer/office-impro/cer-office-impro";
import { IPageInfo } from "hhlt-core/src/model/page/pageInfo";
import dateUtils from "lxsh-cer-web-pc/src/utils/common/dateUtlis";
import { requestContextPath } from 'lxsh-cer-web-pc/src/constants'
import cerOfficeImproApi from "lxsh-cer-web-pc/src/api/cer/cer/cerOfficeImproApi";

interface ICommonFormProps {
  //官方暂不支持import props暂定解决方案，等后期支持后删除，issue #4294
  pageInfo: IPageInfo<ICerOfficeImpro>;
  functionDistinguish: string;
  isEditFormVisible: Boolean;
}
//获取props参数
const props = defineProps<ICommonFormProps>();
//获取form实例
const dataFormRef: Ref<FormInstance | undefined> = ref<FormInstance>();
//自定义事件[本编辑界面处理完成]
const emits = defineEmits(["editProcessSuccess"]);
//hooks初始化
const {
  commonData,
  title,
  dialogPosition,
  dilogHeight,
  isReadOnly,
  beforeCloseDialog,
  onSubmit,
} = hooksForCommonForm<CerOfficeImpro>(
  new CerOfficeImpro(),
  dataFormRef,
  props,
  emits
);
//返回vue界面需要使用到的data
const { dialogWidth, formData, verify } = toRefs(commonData);

onBeforeMount(() => {
  formData.value.week = dateUtils.getCurrentWeek().weekNum;
  formData.value.stratTime = dateUtils.getCurrentWeek().startDate;
  formData.value.endTime = dateUtils.getCurrentWeek().endDate;
});
let handleWeekChange = (e: any) => {
  console.log(e, "=====");
  let rand = dateUtils.getDateForCurrentWeek(e);
  formData.value.stratTime = rand[0];
  formData.value.endTime = rand[1];
};

let lightCoef = 0; //灯的系数
let airCoef = 0; //空调的系数
cerOfficeImproApi.getTypeGroupComboByGroup((res) => {
  if (res.success && res.hasData) {
    res.list.forEach((i) => {
      let text = new String(i.text);
      if (text.indexOf("灯") != -1) {
        if (i.otherInfo) {
          lightCoef = JSON.parse(i.otherInfo).coefficient;
        }
      }
      if (text.indexOf("空调") != -1) {
        if (i.otherInfo) {
          airCoef = JSON.parse(i.otherInfo).coefficient;
        }
      }
    });
  }
});

/**
 * 输入发生改变  自动计算减碳量
 */
const inputChange = () => {
  formData.value.reduceCount = formData.value.lightTime * lightCoef + formData.value.airTime * airCoef 
};
</script>
